package cn.csc.module.distribute.controller.admin.distributor;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.distribute.controller.admin.distributor.vo.*;
import cn.csc.module.distribute.convert.distributor.DistributorConvert;
import cn.csc.module.distribute.dal.dataobject.distributor.DistributorDO;
import cn.csc.module.distribute.service.distributor.DistributorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 分销员信息")
@RestController
@RequestMapping("/distribute/distributor")
@Validated
public class DistributorController {

    @Resource
    private DistributorService distributorService;

    /**
     * 更新分销员信息
     */
    @PutMapping("/update")
    @ApiOperation("更新分销员信息")
    @PreAuthorize("@ss.hasPermission('distribute:distributor:update')")
    public CommonResult<Boolean> updateDistributor(@Valid @RequestBody DistributorUpdateReqVO updateReqVO) {
        distributorService.updateDistributor(updateReqVO);
        return success(true);
    }

    /**
     * 删除分销员信息
     */
    @DeleteMapping("/delete")
    @ApiOperation("删除分销员信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('distribute:distributor:delete')")
    public CommonResult<Boolean> deleteDistributor(@RequestParam("id") BigInteger id) {
        distributorService.deleteDistributor(id);
        return success(true);
    }

    /**
     * 获得分销员信息
     */
    @GetMapping("/get")
    @ApiOperation("获得分销员信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('distribute:distributor:query')")
    public CommonResult<DistributorRespVO> getDistributor(@RequestParam("id") BigInteger id) {
        DistributorDO distributor = distributorService.getDistributor(id);
        return success(DistributorConvert.INSTANCE.convert(distributor));
    }

    /**
     * 获得分销员信息分页
     */
    @GetMapping("/page")
    @ApiOperation("获得分销员信息分页")
    @PreAuthorize("@ss.hasPermission('distribute:distributor:query')")
    public CommonResult<PageResult<DistributorRespVO>> getDistributorPage(@Valid DistributorPageReqVO pageVO) {
        PageResult<DistributorDO> pageResult = distributorService.getDistributorPage(pageVO);
        return success(DistributorConvert.INSTANCE.convertPage(pageResult));
    }

    /**
     * 导出分销员信息 Excel
     */
    @GetMapping("/export-excel")
    @ApiOperation("导出分销员信息 Excel")
    @PreAuthorize("@ss.hasPermission('distribute:distributor:export')")
    @OperateLog(type = EXPORT)
    public void exportDistributorExcel(@Valid DistributorExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<DistributorDO> list = distributorService.getDistributorList(exportReqVO);
        // 导出 Excel
        List<DistributorExcelVO> datas = DistributorConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "分销员信息.xls", "数据", DistributorExcelVO.class, datas);
    }

}
